package com.jiazhong.mingxing.boot.boot04.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;

@Slf4j
@SpringBootTest
public class ZSetApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        forZSet.add("A", "aa", 1.1);
        forZSet.add("A", "bb", 1.7);
        forZSet.add("A", "aa", 1.3);
        forZSet.add("A", "cc", 1.5);
    }

    @Test
    public void b() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        String randomMember = forZSet.randomMember("A");
        log.info("randomMember:{}", randomMember);
        Set<String> range = forZSet.range("A", 0, -1);
        log.info("range:{}", range);
        List<String> randomMembers = forZSet.randomMembers("A", 3);
        log.info("randomMembers:{}", randomMembers);
    }

    @Test
    public void c() {
        ZSetOperations<String, String> forZSet = stringRedisTemplate.opsForZSet();
        forZSet.remove("A", "aa", "kk");
        Long size = forZSet.size("A");
        log.info("size:{}", size);
        forZSet.add("B", "aa", 1.1);
        forZSet.add("B", "bb", 1.2);
        forZSet.add("B", "cc", 1.3);
        forZSet.add("B", "dd", 1.4);
        forZSet.add("C", "bb", 2.2);
        forZSet.add("C", "33", 2.3);
        forZSet.add("C", "dd", 2.4);
        Set<String> union = forZSet.union("B", "C");
        log.info("union:{}", union);
        Set<String> difference = forZSet.difference("B", "C");
        log.info("difference:{}", difference);
        Set<String> intersect = forZSet.intersect("B", "C");
        log.info("intersect:{}", intersect);
    }
}
